package org.jeecg.jxc.base.goods.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.jeecg.jxc.base.goods.entity.Goods;
import org.jeecg.jxc.base.goods.entity.Unit;
import org.jeecg.jxc.base.goods.service.GoodsService;
import org.jeecg.jxc.base.goods.service.GoodsTypeService;
import org.jeecg.jxc.base.goods.vo.PageVo;
import org.jeecg.jxc.base.goods.vo.ServiceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : luojin
 * @date : 2022/4/26 15:42
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品管理",description = "商品管理接口api")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsTypeService goodsTypeService;
    //3.2商品单位列表查询
    @GetMapping("/unit/list")
    public ResponseEntity<Map<String,Object>> getUnit(){
        List<Unit> unitList=goodsService.getUnitList();
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",unitList);
        return ResponseEntity.ok(map);
    }
    //3.3商品列表
    @GetMapping("/list")
    @ApiOperation(value = "商品列表", notes = "根据商品名称和商品id分页查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName", value = "商品名称", required = false, paramType = "path",dataType = "String"),
            @ApiImplicitParam(name = "goodsTypeId", value = "商品分类ID", required = false, paramType = "path",dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "当前页", required = false, paramType = "path",dataType = "Integer"),
            @ApiImplicitParam(name = "rows", value = "每页记录数", required = false, paramType = "path",dataType = "Integer"),
    })
    public ResponseEntity<Map<String,Object>> goodList(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNo,
            @RequestParam(value = "rows",defaultValue = "5")Integer pageSize,
            String goodsName,
            Integer goodsTypeId){

        PageVo<Goods> pageVo=goodsService.pageGoods(pageNo,pageSize,goodsName,goodsTypeId);
        Map<String, Object> map = new HashMap<>();
        //分页对象
        HashMap<String, Object> pageMap = new HashMap<>();
        pageMap.put("total",pageVo.getTotal());
        pageMap.put("pageNo",pageNo);
        pageMap.put("pageSize",pageSize);

        map.put("total", pageVo.getTotal());
        map.put("rows",pageVo.getList());
        map.put("page", pageMap);
        return  ResponseEntity.ok(map);
    }
    //3.4商品分类添加
    @PostMapping("/goodsType/save")
    public ResponseEntity<ServiceVO> addGoodsType(
            @RequestParam("goodsTypeName") String goodsTypeName,
            @RequestParam("pId")Integer pId){
        boolean result=goodsTypeService.save(goodsTypeName,pId);
        ServiceVO serviceVO = new ServiceVO();
        return  ResponseEntity.ok(serviceVO);
    }
    //3.5商品分类删除
    @DeleteMapping("/goodsType/delete")
    public ResponseEntity<ServiceVO> delGoodsType(
            @RequestParam("goodsTypeId") Integer goodsTypeId
           ){
        boolean result=goodsTypeService.delGoodsType(goodsTypeId);
        ServiceVO serviceVO = new ServiceVO();
        return  ResponseEntity.ok(serviceVO);
    }
    //3.6.1 商品添加
    @PostMapping("/save")
    public ResponseEntity<ServiceVO> addGoods(
            @RequestParam("goods") Goods goods
           ){
        boolean result=goodsService.addGoods(goods);
        ServiceVO serviceVO = new ServiceVO();
        serviceVO.setCode("100");
        serviceVO.setInfo(null);
        serviceVO.setMsg("添加成功");
        return  ResponseEntity.ok(serviceVO);
    }
    //3.6.2 商品修改
    @PutMapping("/save")
    public ResponseEntity<ServiceVO> delGoods(
            @RequestParam("goods") Goods goods
           ){
        boolean result=goodsService.updateGoods(goods);
        ServiceVO serviceVO = new ServiceVO();
        serviceVO.setCode("100");
        serviceVO.setInfo(null);
        serviceVO.setMsg("修改成功");
        return  ResponseEntity.ok(serviceVO);
    }



}
